package edu.umn.contactviewer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import edu.umn.contactviewer.utility.ContactViewerUtilities;

public class DetailsActivity extends Activity {

	private static final String FIELD_NAME_VALUE_SEPARATOR = ": ";
	private static int EDIT_CONTACT = 0;

	private Contact displayedContact = null;

	public DetailsActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		new DetailsToolbar();

		displayedContact = (Contact) getIntent().getExtras().get(
				"contactForDetails");

		if (null == displayedContact) {
			Log.e(getClass().getName(), "Null Contact");
		} else {
			updateFields();
		}
	}

	private void updateFields() {
		initializeNameAndTitleDetails();
		initializePhoneDetails();
		initializeEmailDetails();
		initializeTwitterIDDetails();
	}

	private void initializeNameAndTitleDetails() {
		TextView nameTextView = (TextView) findViewById(R.id.details_name);
		nameTextView.setText(displayedContact.getName());
		TextView titleTextView = (TextView) findViewById(R.id.details_title);
		titleTextView.setText(displayedContact.getTitle());
	}

	private void initializePhoneDetails() {
		Button phoneButton = (Button) findViewById(R.id.details_phone);
		phoneButton.setText(getDisplayString(R.string.phone)
				+ displayedContact.getPhone());

		Boolean setPhoneButtonClickable = Boolean.TRUE;
		if (displayedContact.getPhone().length() > 0) {
			phoneButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (ContactViewerUtilities.INSTANCE.isIntentAvailable(
							getApplicationContext(), Intent.ACTION_CALL)) {
						Intent phoneCallIntent = new Intent(Intent.ACTION_CALL,
								Uri.parse(displayedContact.getPhone()));
						startActivity(phoneCallIntent);
					} else {
						Toast.makeText(getApplicationContext(),
								R.string.phone_activity_unavailable_message, 5)
								.show();
					}
				}
			});
		} else {
			setPhoneButtonClickable = Boolean.FALSE;
		}

		phoneButton.setClickable(setPhoneButtonClickable.booleanValue());
	}

	private void initializeEmailDetails() {
		Button emailButton = (Button) findViewById(R.id.details_email);
		emailButton.setText(getDisplayString(R.string.email)
				+ displayedContact.getEmail());

		Boolean setEmailButtonClickable = Boolean.TRUE;
		if (displayedContact.getEmail().length() > 0) {
			emailButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (ContactViewerUtilities.INSTANCE.isIntentAvailable(
							getApplicationContext(), Intent.ACTION_SEND)) {
						Intent emailIntent = new Intent(Intent.ACTION_SEND, Uri
								.parse(displayedContact.getEmail()));
						startActivity(emailIntent);
					} else {
						Toast.makeText(getApplicationContext(),
								R.string.email_activity_unavailable_message, 5)
								.show();
					}
				}
			});
		} else {
			setEmailButtonClickable = Boolean.FALSE;
		}

		emailButton.setClickable(setEmailButtonClickable.booleanValue());
	}

	private void initializeTwitterIDDetails() {
		Button twitterIDButton = (Button) findViewById(R.id.details_twitter_id);
		twitterIDButton.setText(getDisplayString(R.string.twitter_id)
				+ displayedContact.getTwitterId());

		Boolean setTwitterIDButtonClickable = Boolean.TRUE;
		if (displayedContact.getTwitterId().length() > 0) {
			twitterIDButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (ContactViewerUtilities.INSTANCE.isIntentAvailable(
							getApplicationContext(), Intent.ACTION_VIEW)) {
						Intent twitterIntent = new Intent(Intent.ACTION_VIEW,
								Uri.parse(getString(R.string.twitter_url)
										+ displayedContact.getTwitterId()));
						startActivity(twitterIntent);
					} else {
						Toast.makeText(getApplicationContext(),
								R.string.twitter_activity_unavailable_message,
								5).show();
					}
				}
			});
		} else {
			setTwitterIDButtonClickable = Boolean.FALSE;
		}

		twitterIDButton
				.setClickable(setTwitterIDButtonClickable.booleanValue());
	}

	private String getDisplayString(int idOfDisplayStringToRetrieve) {
		return getString(idOfDisplayStringToRetrieve)
				+ FIELD_NAME_VALUE_SEPARATOR;
	}

	private class DetailsToolbar extends ToolbarConfig {
		public DetailsToolbar() {
			super(DetailsActivity.this, getString(R.string.details));
			Button rightButton = getToolbarRightButton();
			rightButton.setText(getString(R.string.edit));
			rightButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent editIntent = new Intent(DetailsActivity.this,
							EditActivity.class);
					editIntent.putExtra("contactToEdit", displayedContact);
					startActivityForResult(editIntent, EDIT_CONTACT);
				}
			});
			rightButton.setVisibility(View.VISIBLE);
		}

	}

	@Override
	// for handling returns from create
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (EDIT_CONTACT == requestCode && RESULT_OK == resultCode
				&& null != data) {
			// retrieve the updated contact to update the display
			displayedContact = (Contact) data.getExtras().get(
					"contactForDetailsResults");

			// update the display
			updateFields();

			// set result to inform list activity of change when the activity
			// finishes
			setResult(RESULT_OK);
		}// end if (CREATE_CONTACT == requestCode && RESULT_OK == resultCode)
	}
}
